<?php


namespace app\admin\controller;

use app\admin\controller\ApiCommon;
use app\admin\model\GoodsCategory as CategoryModel;
use think\response\Json;
use app\common\traits\ActionRecordTrait;

/**
 * Class Category
 * @package app\shop\controller
 * @author Mr.Liu
 * @create 2021-11-02 10:16
 */
class Category extends ApiCommon
{
    use ActionRecordTrait;
    /**
     * 用于判断权限
     * @permission 无限制
     * @allow 登录用户可访问
     * @author Mr.Liu
     * @create 2021-11-02 10:54
     */
    public function initialize()
    {

        $action = [
            'permission' => [''],
            'allow' => [
                'all'
            ]
        ];
        event('CheckAuth', $action);
        $request = $this->request;
        $a       = strtolower($request->action());
        if (!in_array($a, $action['permission'])) {
            parent::initialize();
        }
    }

    /**
     * 分类列表
     * @return Json
     * @author Mr.Liu
     * @create 2021-11-02 10:53
     */
    public function index(): Json
    {
        $categoryModel = new CategoryModel();
        $data          = $categoryModel->getDataList($this->param);
        return resultArray(['data' => $data]);
    }

    /**
     * 新增分类
     * @return Json
     * @author Mr.Liu
     * @create 2021-11-02 11:26
     */
    public function create(): Json
    {
        $categoryModel = new CategoryModel();
        $this->param['user'] = $this->userInfo;
        $data          = $categoryModel->addData($this->param);
        if (!$data) {
            return resultArray(['error' => $categoryModel->getError()]);
        }
        return resultArray(['data' => [],'error' => '创建成功']);
    }

    /**
     * 更新分类
     * @return Json
     * @author Mr.Liu
     * @create 2021-11-02 11:33
     */
    public function update(): Json
    {
        $categoryModel = new CategoryModel();
        $this->param['user'] = $this->userInfo;
        $data          = $categoryModel->saveData($this->param);
        if (!$data) {
            return resultArray(['error' => $categoryModel->getError()]);
        }
        return resultArray(['data' => [],'error' => '更新成功']);
    }

    /**
     * 删除分类
     * @return Json
     * @author Mr.Liu
     * @create 2021-11-02 14:53
     */
    public function delete(): Json
    {
        $categoryModel = new CategoryModel();
        $this->param['user'] = $this->userInfo;
        $data          = $categoryModel->deleteData($this->param);
        if (!$data) {
            return resultArray(['error' => $categoryModel->getError()]);
        }
        return resultArray(['data' => [],'error' => '删除成功']);
    }

    /**
     * 商品分类详情
     * @return Json
     * @author Mr.Li
     * @create 2021-06-09 14:31
     */
    public function detail(): Json
    {
        $categoryModel = new CategoryModel();
        $data          = $categoryModel->getDataDetail($this->param);
        return resultArray(['data' => $data]);
    }


    /**
     * 所有分类列表
     * @return Json
     * @author Mr.Li
     * @create 2021-06-29 9:39
     */
    public function all(): Json
    {
        $categoryModel = new CategoryModel();
        $this->param['to_page'] = 1;
        $data          = $categoryModel->getDataList($this->param);
        return resultArray(['data' => $data]);
    }

    /**
     * 操作日志
     * @return Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author Mr.Li
     * @create 2021-08-25 16:33
     */
    public function log()
    {
        $specId = $this->param['id'] ?? 0;
        $page = $this->param['page'] ?? 1;
        $limit = $this->param['limit'] ?? 20000;
        if(!$page){
            $page = 1;
        }
        $data = [];
        $total = 0;
        if($specId){
            $total = $this->getActionRecordTotal($specId,'shop','goods_category  ');
            $data = $this->getActionRecordList($specId,'shop','goods_category  ','',$page,$limit);
        }
        return resultArray(['data' => ['list' => $data,'dataCount' => $total ]]);

    }


}
